package vampirism.core.shared;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class Tile {
	
	public Index index;	
	public int biomeNum;
	public Rectangle pos;
	public List<Player> players = new ArrayList<Player>();
	public List<Bullet> bullets = new ArrayList<Bullet>();
	public boolean solid = false;
	public TextureRegion texture;
	
	static Random rand = new Random(Network.seed);
	
	public static Tile[][] createMap() {
		
		FastNoise[] noise = new FastNoise[15];
		
		//Initialize noise map generator
        for(int i = 0; i < noise.length; i++){
        	//The seeds for the Random() are always the same (i+1)
        	//So the map will always be initialized equally on server side and locally! (there won't be any differences)
            noise[i]=new FastNoise(new Random(Network.seed+i+1));
            noise[i].randomize();
        }
		
		//The same random for each time we create this map		
		rand = new Random(Network.seed);
		
		//offsetting the map - so that the point (0,0) is in the center
		Tile[][] map = new Tile[Network.MAP_W][Network.MAP_H];
		
		for(int i = 0; i < Network.MAP_W; i++){
			for(int j = 0; j < Network.MAP_H; j++){
				map[i][j] = new Tile();
				map[i][j].pos = new Rectangle(i*8 - Network.halfMapWidth,j*8 - Network.halfMapHeight,8,8);
				map[i][j].index = new Index(i,j);
				map[i][j].biomeNum = getBiomeNum(map[i][j].pos.x,map[i][j].pos.y, noise, true);
				
				//Generate walls at random locations, but not in the starting area (14)
				if(rand.nextInt(1000)<100 && map[i][j].biomeNum!=14)map[i][j].solid = true;
			}
		}
		
		
		
		return map;
	}
	
	public static int getBiomeNum(float x, float y, FastNoise[] noise, boolean allowStartingArea){
		
		int biomeNum=0;
		int intensity=0;
		
	     //Default Grass (below everything)
        intensity = noise[1].noise(x/1300f,y/1300f,5);
        if(intensity < 115)biomeNum = 0;else
        if(intensity >= 115)biomeNum = 1;

        //Dirt Brown (random areas)
        intensity = noise[2].noise(x/1109f,y/1109f,5);
        if(intensity >= 145)biomeNum = 2;

        //Dirt Gray (random areas)
        intensity = noise[3].noise(x/1109f,y/1109f,5);
        if(intensity >= 145)biomeNum = 3;

        //Rivers
        intensity = noise[4].noise(x/1009f,y/1009f,5);
        if(intensity > 122 && intensity < 125)biomeNum = 4;

        //Roads - wooden
        intensity = noise[6].noise(x/800f,y/800f,5);
        if(intensity > 125 && intensity < 127)biomeNum = 6;

        //Roads - concrete
        intensity = noise[7].noise(x/700f,y/700f,5);
        if(intensity > 125 && intensity < 127)biomeNum = 7;

        //Lakes/Oceans  (roads go over rivers but not over oceans)
        intensity = noise[5].noise(x/1009f,y/1009f,5);
        if(intensity <= 115 && intensity > 112)biomeNum = 4;else
        if(intensity <= 112)biomeNum = 5;

        //Desert - sand/solid
        //intensity = noise[8].noise(x/2838f,y/2838f,5);
        //if(intensity <= 90 && intensity > 87)biome = 8;else
        //if(intensity <= 87)biome = 9;

        //Ice - normal/squared
        //intensity = noise[9].noise(x/2838f,y/2838f,5);
        //if(intensity <= 90 && intensity > 87)biome = 10;else
        //if(intensity <= 87)biome = 11;

        //Godland - gray/black
        //intensity = noise[10].noise(x/2138f,y/2138f,5);
        //if(intensity <= 90 && intensity > 87)biome = 12;else
        //if(intensity <= 87)biome = 13;

        //if(intensity >= 140 && intensity < 150)biome = 8; //Almost godland
        //if(intensity >= 150)biome = 9; //Godland
        if(allowStartingArea)
        if(x > -64 && x < 64 && y > -64 && y < 64){
        	biomeNum = 14;//Start area
        }

        return biomeNum;
	}
}
